﻿using NetMQ;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NETMQLib
{
    /// <summary>
    /// 发布mq
    /// </summary>
    public class PubMQ
    {
        NetMQ.Sockets.PublisherSocket socket = null;
        public BaseMQModel BaseMQInfo { get; set; }

        public PubMQ(string ip, int port)
        {
            try
            {
                BaseMQInfo = new BaseMQModel() { IP = ip, Port = port };
                string url = $"tcp://{ip}:{port}";
                socket = new NetMQ.Sockets.PublisherSocket();
                socket.Bind(url);
            }
            catch (Exception ex)
            {
                //throw ex.InnerException;
            }
        }

        public void SendData(string msg, string topic = "")
        {
            socket.SendMoreFrame(topic).SendFrame(msg);
        }

        public void Dispose()
        {
            if (socket != null && !socket.IsDisposed)
            {
                socket.Dispose();
                socket.Close();
            }
        }
    }
}
